//
//  StudyRankingPersonInformationViewController.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/3.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "StudyRankingPersonInformationViewController.h"
#import "CommonStudyLinkHeaderView.h"
#import "TenGrayShadowView.h"
#import "MenuButtonView.h"
#import "ZJStudyRecommendModel.h"
/** 视频、音频cell */
#import "ZJStudyVideoTableViewCell.h"
/** 图书cell */
#import "ZJStudyBookTableViewCell.h"
#import "StudyCoureVideoViewController.h"
@interface StudyRankingPersonInformationViewController ()<UITableViewDataSource,UITableViewDelegate,MenuButtonDelegate>
/**  title  */
@property (strong, nonatomic) NSArray  *titlesArray;

/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  page  */
@property (assign, nonatomic) NSInteger  page;
/**  最近学习数据  */
@property (strong, nonatomic) NSMutableArray  *recentlyDataSouce;
/**  共同学习数据  */
@property (strong, nonatomic) NSMutableArray  *commonDataSouce;
/**  朋友的信息  */
@property (strong, nonatomic) NSDictionary  *friendDic;

/**  判断是共同学习还是最近学习  */
@property (strong, nonatomic) NSString  *judgeString;

/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;

@end

@implementation StudyRankingPersonInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = [NSString stringWithFormat:@"%@的学习",self.personModel.USER_NAME];
    self.titlesArray = @[@"最近学习",@"共同学习"];
    self.judgeString = self.titlesArray[0];
    self.page = 1;
    
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    
    /**  刚进页面显示最近学习的数据,并且不能刷新  */
    [self getData];
    self.tableView.mj_header.hidden = YES;
    self.tableView.mj_footer.hidden = YES;
    [self layoutUI];
}

- (void)getData{
    [YJProgressHUD showMessage:@"加载中..." duration:10 view:self.view];
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"PAGE_NUMBER": page,
                              @"FD_STUDENTID": self.personModel.STUDENTID,
                              @"ME_STUDENTID": self.myModel.STUDENTID,
                              @"SHOW_NUMBER": @"5"};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFriendRankinggetFirendInformation] Parameter:paramer Success:^(id object) {
        [YJProgressHUD hide];
        if ([object[@"STATE"] isEqualToString:@"20000"]) {
            
            self.friendDic = object[@"FRIEND"];
            NSArray *array1 = object[@"FRIEND_ST_VIDEO_LOG"];
            if (array1.count == 0) {
                [YJProgressHUD showMessage:@"暂无学习记录" inView:[UIApplication sharedApplication].keyWindow];
            }
            [self.recentlyDataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[ZJStudyRecommendModel class] json:array1]];
            [self.tableView reloadData];
            
        }
        
    } Fail:^(id object) {
       [YJProgressHUD hide];
    }];

}

#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
   
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"PAGE_NUMBER": page,
                              @"FD_STUDENTID": self.personModel.STUDENTID,
                              @"ME_STUDENTID": self.myModel.STUDENTID,
                              @"SHOW_NUMBER": @"5"};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFriendRankinggetFirendInformation] Parameter:paramer Success:^(id object) {
        [YJProgressHUD hide];
        if ([object[@"STATE"] isEqualToString:@"20000"]) {
            
        NSArray *array = object[@"ST_VIDEO_LOG"];
                
        weakSelf.commonDataSouce = [[NSArray yy_modelArrayWithClass:[ZJStudyRecommendModel class] json:array] mutableCopy];
        
        }else{
             [self.commonDataSouce removeAllObjects];
        }
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [YJProgressHUD hide];
         [self.commonDataSouce removeAllObjects];
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView reloadData];
    }];

    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"PAGE_NUMBER": page,
                              @"FD_STUDENTID": self.personModel.STUDENTID,
                              @"ME_STUDENTID": self.myModel.STUDENTID,
                              @"SHOW_NUMBER": @"5"};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFriendRankinggetFirendInformation] Parameter:paramer Success:^(id object) {
        NetWorkModel *mode = [NetWorkModel yy_modelWithJSON:object];
        if ([mode.STATE isEqualToString:@"20000"]) {
            NSArray *array = object[@"ST_VIDEO_LOG"];
        if (array.count == 0) {
            [YJProgressHUD showMessage:@"没有数据" inView:[UIApplication sharedApplication].keyWindow];
        }
            
        [weakSelf.commonDataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[ZJStudyRecommendModel class] json:array]];
        [weakSelf.tableView reloadData];
        
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - 界面
- (void)layoutUI{
    /**  头部标签  */
    CommonStudyLinkHeaderView *header = [[CommonStudyLinkHeaderView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, [UIScreen zj_screenWith], 220 * [UIScreen zj_screenHScale])];
    header.isName = YES;
    header.model = self.personModel;
    [self.view addSubview:header];
    
    /**  标题  */
    MenuButtonView *menu = [[MenuButtonView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(header.frame), [UIScreen zj_screenWith], 45 * [UIScreen zj_screenHScale]) titlesArray:self.titlesArray];
    menu.delegate = self;
    menu.underLine.hidden = YES;
    [self.view addSubview:menu];
    
    
    /**  添加tableVIew  */
    [self.view addSubview:self.tableView];
    TenGrayShadowView *tne = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
    self.tableView.tableHeaderView = tne;
}


#pragma mark - MenuButtonDelegate
- (void)menuButtonWithView:(MenuButtonView *)view buttonTitle:(NSString *)buttonTitle tag:(NSInteger)tag{
    self.judgeString = buttonTitle;
    if ([buttonTitle isEqualToString:@"最近学习"]) {
        self.tableView.mj_header.hidden = YES;
        self.tableView.mj_footer.hidden = YES;
        [self.tableView reloadData];
        
    }else{
        self.tableView.mj_header.hidden = NO;
        self.tableView.mj_footer.hidden = NO;
        [self.tableView.mj_header beginRefreshing];
    }
    
}


#pragma mark -   UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.judgeString isEqualToString:@"最近学习"]) {
        return self.recentlyDataSouce.count;
    }else{
        return self.commonDataSouce.count;
    }
    
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZJStudySpacialDetailModel * model;
    if ([self.judgeString isEqualToString:@"最近学习"]) {
        model = self.recentlyDataSouce[indexPath.row];
    }else{
        model = self.commonDataSouce[indexPath.row];
    }
    if ([model.COURSETYPE isEqualToString:@"4"]){//悦读会
        return 125 * [UIScreen zj_screenHScale];//图书
    }
    return 120 * [UIScreen zj_screenHScale];//视频、音频
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    ZJStudyRecommendModel * model;
    if ([self.judgeString isEqualToString:@"最近学习"]) {
        model = self.recentlyDataSouce[indexPath.row];
    }else{
        model = self.commonDataSouce[indexPath.row];
    }
    
    
    if ([model.COURSETYPE isEqualToString:@"4"]) {//悦读会
        ZJStudyBookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[ZJStudyBookTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        
        cell.model = model;
        return cell;
    }else{
        
        ZJStudyVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[ZJStudyVideoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;
        
    }
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     StudyCoureVideoViewController *vc = [StudyCoureVideoViewController new];
    if ([self.judgeString isEqualToString:@"最近学习"]) {
         vc.model = self.recentlyDataSouce[indexPath.row];
    }else{
         vc.model = self.commonDataSouce[indexPath.row];
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight + 265 * [UIScreen zj_screenHScale], self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight - 265 * [UIScreen zj_screenHScale]) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}


- (NSMutableArray *)recentlyDataSouce{
    if (!_recentlyDataSouce) {
        _recentlyDataSouce = [NSMutableArray array];
    }
    return _recentlyDataSouce;
}

- (NSMutableArray *)commonDataSouce{
    if (!_commonDataSouce) {
        _commonDataSouce = [NSMutableArray array];
    }
    return _commonDataSouce;
}


- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}



@end
